package com.kitty.game.equip.service;

import com.kitty.common.cache.RedisService;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.chat.service.ChatService;
import com.kitty.game.config.Equip;
import com.kitty.game.confirm.model.GSGConfirm;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.equip.UpgradeType;
import com.kitty.game.equip.handler.EquipUpgradeHandler;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespConfirm;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.EquipUpgradeLevelChangeEvent;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.message.MessagePusher;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

@Service
public class GSGService {
    @Autowired
    RoleService roleService;
    @Autowired
    EquipService equipService;
    @Autowired
    ChatService chatService;
    LinkedHashMap<String, NutMap> data = new LinkedHashMap();
    private int npcId = 14879;
    @Autowired
    RedisService redisService;
    public void init() {

        {
            NutMap nutMap = new NutMap();
            nutMap.setv("msg", "gsg000051");
            nutMap.setv("jifen",  Integer.parseInt(DataCache.REDIS_DATA.get("gai6-gai7")));
            nutMap.setv("name", "改6-改7(一套四件)");
            nutMap.setv("min", 6);
            nutMap.setv("max", 7);
            data.put(nutMap.getString("name"), nutMap);
        }
        {
            NutMap nutMap = new NutMap();
            nutMap.setv("msg", "gsg000051");
            nutMap.setv("jifen", Integer.parseInt(DataCache.REDIS_DATA.get("gai7-gai8")));
            nutMap.setv("name", "改7-改8(一套四件)");
            nutMap.setv("min", 7);
            nutMap.setv("max", 8);
            data.put(nutMap.getString("name"), nutMap);
        }
        {
            NutMap nutMap = new NutMap();
            nutMap.setv("msg", "gsg00005");
            nutMap.setv("jifen", Integer.parseInt(DataCache.REDIS_DATA.get("gai8-gai9")));
            nutMap.setv("name", "改8-改9(一套四件)");
            nutMap.setv("min", 8);
            nutMap.setv("max", 9);
            data.put(nutMap.getString("name"), nutMap);
        }
        {
            NutMap nutMap = new NutMap();
            nutMap.setv("msg", "gsg00005");
            nutMap.setv("jifen", Integer.parseInt(DataCache.REDIS_DATA.get("gai9-gai10")));
            nutMap.setv("name", "改9-改10(一套四件)");
            nutMap.setv("min", 9);
            nutMap.setv("max", 10);
            data.put(nutMap.getString("name"), nutMap);
        }
        {
            NutMap nutMap = new NutMap();
            nutMap.setv("msg", "gsg00009");
            nutMap.setv("jifen", Integer.parseInt(DataCache.REDIS_DATA.get("gai10-gai11")));
            nutMap.setv("name", "改10-改11(一套四件)");
            nutMap.setv("min", 10);
            nutMap.setv("max", 11);
            data.put(nutMap.getString("name"), nutMap);
        }
        {
            NutMap nutMap = new NutMap();
            nutMap.setv("msg", "gsg000013");
            nutMap.setv("jifen", Integer.parseInt(DataCache.REDIS_DATA.get("gai11-gai12")));
            nutMap.setv("name", "改11-改12(一套四件)");
            nutMap.setv("min", 11);
            nutMap.setv("max", 12);
            data.put(nutMap.getString("name"), nutMap);
        }
    }


    public String pointGSG() {
        String head = "装备通过改造可以迅速提升伤害和防御。";
        for (Map.Entry<String, NutMap> entry : data.entrySet()) {
            NutMap nutMap = entry.getValue();
            head = head + "[" + nutMap.getString("name") + "]";
        }
        String tail = "[我要改造装备][离开/离开]";
        return head + tail;
    }


    public boolean buy(Role role, String msg) {
        String subType = DataCache.REDIS_DATA.get("zhengtaogaizao_xiaohao");
        NutMap nutMap = data.get(msg);
        if (nutMap == null) {
            return true;
        }
        role.setConfirm(new GSGConfirm(msg));
        msg = "确认花费#R" + nutMap.getInt("jifen") + "#n"+subType+",完成#R" + nutMap.getString("name") + "#n？";
        RespConfirm respConfirm = new RespConfirm();
        respConfirm.setTips(msg);
        MessagePusher.pushMessage(role, respConfirm);
        return true;
    }

    public boolean besureBuy(Role role) {
        GSGConfirm gsgConfirm = (GSGConfirm) role.getConfirm();
        String msgId = gsgConfirm.getMsg();
        NutMap nutMap = data.get(msgId);
        if (nutMap == null) {
            return true;
        }
        String subType = DataCache.REDIS_DATA.get("zhengtaogaizao_xiaohao");
        int min = nutMap.getInt("min");
        int max = nutMap.getInt("max");
        int cost = nutMap.getInt("jifen",100000000);
        EquipUpgradeHandler equipUpgradeHandler = SpringUtils.getBean("EquipUpgrade", EquipUpgradeHandler.class);
        RoleEquip wuqi = equipService.getRoleEquipByPos(role, 1);
        if (msgId.contains("武器")) {
            if (wuqi == null) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("武器没有佩戴在身上！"));
                return true;
            }
            if (wuqi.queryUpgradeLevel() < min) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("武器改造等级不到#R改" + min + "#n！"));
                return true;
            }
            if (wuqi.queryUpgradeLevel() >= max) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("武器改造等级超过#R改" + max + "#n！"));
                return true;
            }
            //roleService.substractChargeScore(role, cost);
            roleService.subCountByteType(role,cost, subType);
            gaizao(wuqi, (short) max, role, equipUpgradeHandler, EquipDataPool.getByName(wuqi.getName()));
            String notice = "惊闻#R" + role.getName() + "#n玩家在#R管神工#n处将装备#R" + wuqi.getName() + "#n改造等级提升至#R改" + max + "#n，真是可喜可贺！#51 #82";
            chatService.sendAdnotice(notice);
            return false;
        }
        if (msgId.contains("衣服")) {
            RoleEquip yifu = equipService.getRoleEquipByPos(role, 3);
            if (yifu == null) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("衣服没有佩戴在身上！"));
                return true;
            }
            if (yifu.queryUpgradeLevel() < min) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("衣服改造等级不到#R改" + min + "#n！"));
                return true;
            }
            if (yifu.queryUpgradeLevel() >= max) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("衣服改造等级超过#R改" + max + "#n！"));
                return true;
            }
            //roleService.substractChargeScore(role, cost);
            roleService.subCountByteType(role,cost, subType);
            gaizao(yifu, (short) max, role, equipUpgradeHandler, EquipDataPool.getByName(yifu.getName()));
            String notice = "惊闻#R" + role.getName() + "#n玩家在#R管神工#n处将装备#R" + yifu.getName() + "#n改造等级提升至#R改" + max + "#n，真是可喜可贺！#51 #82";
            chatService.sendAdnotice(notice);
            return false;
        }
        if (msgId.contains("帽子")) {
            RoleEquip maozi = equipService.getRoleEquipByPos(role, 2);
            if (maozi == null) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("帽子没有佩戴在身上！"));
                return true;
            }
            if (maozi.queryUpgradeLevel() < min) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("帽子改造等级不到#R改" + min + "#n！"));
                return true;
            }
            if (maozi.queryUpgradeLevel() >= max) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("帽子改造等级超过#R改" + max + "#n！"));
                return true;
            }
           // roleService.substractChargeScore(role, cost);
            roleService.subCountByteType(role,cost, subType);
            gaizao(maozi, (short) max, role, equipUpgradeHandler, EquipDataPool.getByName(maozi.getName()));
            String notice = "惊闻#R" + role.getName() + "#n玩家在#R管神工#n处将装备#R" + maozi.getName() + "#n改造等级提升至#R改" + max + "#n，真是可喜可贺！#51 #82";
            chatService.sendAdnotice(notice);
            return false;
        }
        if (msgId.contains("鞋子")) {
            RoleEquip xiezi = equipService.getRoleEquipByPos(role, 10);
            if (xiezi == null) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("鞋子没有佩戴在身上！"));
                return true;
            }
            if (xiezi.queryUpgradeLevel() < min) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("鞋子改造等级不到#R改" + min + "#n！"));
                return true;
            }
            if (xiezi.queryUpgradeLevel() >= max) {
                MessagePusher.pushMessage(role, new RespNotifyMiscEx("鞋子改造等级超过#R改" + max + "#n！"));
                return true;
            }
           // roleService.substractChargeScore(role, cost);
            roleService.subCountByteType(role,cost, subType);
            gaizao(xiezi, (short) max, role, equipUpgradeHandler, EquipDataPool.getByName(xiezi.getName()));
            String notice = "惊闻#R" + role.getName() + "#n玩家在#R管神工#n处将装备#R" + xiezi.getName() + "#n改造等级提升至#R改" + max + "#n，真是可喜可贺！#51 #82";
            chatService.sendAdnotice(notice);
            return false;
        }


        if (wuqi == null) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("武器没有佩戴在身上！"));
            return true;
        }
        if (wuqi.queryUpgradeLevel() < min) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("武器改造等级不到#R改" + min + "#n！"));
            return true;
        }
        if (wuqi.queryUpgradeLevel() >= max) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("武器改造等级超过#R改" + max + "#n！"));
            return true;
        }
        RoleEquip maozi = equipService.getRoleEquipByPos(role, 2);
        if (maozi == null) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("帽子没有佩戴在身上！"));
            return true;
        }
        if (maozi.queryUpgradeLevel() < min) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("帽子改造等级不到#R改" + min + "#n！"));
            return true;
        }
        if (maozi.queryUpgradeLevel() >= max) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("帽子改造等级超过#R改" + max + "#n！"));
            return true;
        }
        RoleEquip xiezi = equipService.getRoleEquipByPos(role, 10);
        if (xiezi == null) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("鞋子没有佩戴在身上！"));
            return true;
        }
        if (xiezi.queryUpgradeLevel() < min) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("鞋子改造等级不到#R改" + min + "#n！"));
            return true;
        }
        if (xiezi.queryUpgradeLevel() >= max) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("鞋子改造等级超过#R改" + max + "#n！"));
            return true;
        }
        RoleEquip yifu = equipService.getRoleEquipByPos(role, 3);
        if (yifu == null) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("衣服没有佩戴在身上！"));
            return true;
        }
        if (yifu.queryUpgradeLevel() < min) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("衣服改造等级不到#R改" + min + "#n！"));
            return true;
        }
        if (yifu.queryUpgradeLevel() >= max) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("衣服改造等级超过#R改" + max + "#n！"));
            return true;
        }
        //roleService.substractChargeScore(role, cost);
        roleService.subCountByteType(role,cost, subType);
        gaizao(wuqi, (short) max, role, equipUpgradeHandler, EquipDataPool.getByName(wuqi.getName()));
        String notice = "惊闻#R" + role.getName() + "#n玩家在#R管神工#n处将装备#R" + wuqi.getName() + "#n改造等级提升至#R改" + max + "#n，真是可喜可贺！#51 #82";
        chatService.sendAdnotice(notice);
        gaizao(maozi, (short) max, role, equipUpgradeHandler, EquipDataPool.getByName(maozi.getName()));
        notice = "惊闻#R" + role.getName() + "#n玩家在#R管神工#n处将装备#R" + maozi.getName() + "#n改造等级提升至#R改" + max + "#n，真是可喜可贺！#51 #82";
        chatService.sendAdnotice(notice);
        gaizao(xiezi, (short) max, role, equipUpgradeHandler, EquipDataPool.getByName(xiezi.getName()));
        notice = "惊闻#R" + role.getName() + "#n玩家在#R管神工#n处将装备#R" + xiezi.getName() + "#n改造等级提升至#R改" + max + "#n，真是可喜可贺！#51 #82";
        chatService.sendAdnotice(notice);
        gaizao(yifu, (short) max, role, equipUpgradeHandler, EquipDataPool.getByName(yifu.getName()));
        notice = "惊闻#R" + role.getName() + "#n玩家在#R管神工#n处将装备#R" + yifu.getName() + "#n改造等级提升至#R改" + max + "#n，真是可喜可贺！#51 #82";
        chatService.sendAdnotice(notice);
        return true;
    }

    private void gaizao(RoleEquip roleEquip, short currUpgradeLevel, Role role, EquipUpgradeHandler equipUpgradeHandler, Equip equip) {
        /**改造等级+1*/
        roleEquip.alterUpgradeLevel(currUpgradeLevel);
        /**改造进度清0*/
        roleEquip.alterUpgradeProgress(0);
        /**改造进度*/
        roleEquip.setGaizaoJindu(0);

        /**判断当前的改造成功是否需要发谣言*/
        if (currUpgradeLevel > 5) {
            String msg;
            if (currUpgradeLevel > 8) {
                msg = "惊闻玩家#Y%s#n成功将#R{%s}#n的改造等级提升到#R%d级#n了，得此神器，夫复何求啊！#49m";
            } else if (currUpgradeLevel == 8) {
                msg = "#35m这难道就是传说中的神器吗？你们看，这就是#Y%s#n已经成功将#R{%s}#n改#R%d#n了......";
            } else if (currUpgradeLevel == 7) {
                msg = "#63m天呐！#Y%s#n居然成功将#R{%s}#n的改造等级提升到#R%d级#n了！";
            } else {
                msg = "#41m快来看啊！#Y%s#n成功将#R{%s}#n的改造等级提升到#R%d级#n了。";
            }
            equipService.sendEquipMsg(role, roleEquip, currUpgradeLevel, msg);
        }

        equipUpgradeHandler.pushOperateResult(role, equipUpgradeHandler.UPGRADE_SUCCESS);
        /**增加改造成功属性*/
        equipService.addUpgradeField(equip, roleEquip);
        /**计算完美度*/
        equipService.countPerfect(roleEquip);
        /**刷新装备*/
        equipUpgradeHandler.refreshRoleEquip(role, roleEquip);
        /**发下等级属性预览*/
        equipService.equipPreview(role, equip, roleEquip, UpgradeType.UPGRADE_EQUIP_UPGRADE);

        /**产生一个装备改造等级改变事件*/
        EventDispatcher.getInstance().fireEvent(new EquipUpgradeLevelChangeEvent(EventType.EQUIP_UPGRADE_LEVEL_CHANGE, role, roleEquip, currUpgradeLevel));
    }

}
